package com.example.springbootdemo.controller;

import com.example.springbootdemo.annotation.IgnoreWrapper;
import com.example.springbootdemo.annotation.ProcessInfo;
import com.example.springbootdemo.entity.User;
import com.example.springbootdemo.model.TableData;
import com.example.springbootdemo.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

// @CrossOrigin(origins = "*")   options
//@CrossOrigin(allowCredentials = "true", origins = "*")   // allowCredentials = "true" 意思是其他的服务器在访问接口可以携带 cookie
@RestController
@RequestMapping("/user")
public class UserController {

    private IUserService userService;

    public UserController(IUserService userService) {
        this.userService = userService;
    }

    // {"total": 43, "rows": [{}, {}]}  -> {"code": 1, "msg": "", "datas": {"total": 43, "rows": [{}, {}]}}
    @GetMapping
    @IgnoreWrapper
    @ProcessInfo("分页查询用户的信息")
    public TableData<User> getPageData(@RequestParam(defaultValue = "0") Integer offset,
                                       @RequestParam(defaultValue = "10") Integer limit, HttpSession session) {
//        session.setAttribute("userInfo", "zhangsan");
        System.out.println(session.getAttribute("userInfo"));
        return userService.getPageData(offset, limit);
    }

    /**
     * 在springboot中, 如果我们程序中一直没有显式的去使用 HttpSession, 内嵌的tomcat不会去帮我们生成
     *  session.
     */
    // {id: 1, name: ""} ->  {"code": 1, "msg": "", "datas": {"code": 1, "msg": "success", datas: {}}}
    @GetMapping("/{id}")
    @ProcessInfo("根据用户Id获取用户信息")
    public User getUser(@PathVariable("id") Integer id) {
//        System.out.println(session.getAttribute("userInfo"));
        return userService.getUser(id);
    }
}
